package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * ShoppingCartService
 *
 * @author liudo
 * @version 1.0
 * @project sky-take-out
 * @date 2023/12/10 14:15:18
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 是菜品
        if (Objects.nonNull(shoppingCartDTO.getDishId())) {
            // 先判断这个东西有没有 - 怎么查？ -- 唯一性确认 - 谁
            ShoppingCart shoppingCartQuery = new ShoppingCart();
            shoppingCartQuery.setUserId(BaseContext.getCurrentId());
            shoppingCartQuery.setDishId(shoppingCartDTO.getDishId());
            shoppingCartQuery.setDishFlavor(shoppingCartDTO.getDishFlavor());
            ShoppingCart shoppingCart =  shoppingCartMapper.selectOne(shoppingCartQuery);
            // 如果以前没有加购过，就是新增
            if (Objects.isNull(shoppingCart)) {
                shoppingCart = new ShoppingCart();
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                // 数量
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            } else {
                // 如果以前加购过，就是更新数量
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                shoppingCartMapper.updateById(shoppingCart);
            }
        }
        // 是套餐
        if (Objects.nonNull(shoppingCartDTO.getSetmealId())) {
            // 先判断这个东西有没有 - 怎么查？ -- 唯一性确认 - 谁
            ShoppingCart shoppingCartQuery = new ShoppingCart();
            shoppingCartQuery.setUserId(BaseContext.getCurrentId());
            shoppingCartQuery.setSetmealId(shoppingCartDTO.getSetmealId());
            ShoppingCart shoppingCart =  shoppingCartMapper.selectOne(shoppingCartQuery);
            // 如果以前没有加购过，就是新增
            if (Objects.isNull(shoppingCart)) {
                shoppingCart = new ShoppingCart();
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                // 数量
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            } else {
                // 如果以前加购过，就是更新数量
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                shoppingCartMapper.updateById(shoppingCart);
            }
        }

    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCartQuery = new ShoppingCart();
        shoppingCartQuery.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.selectList(shoppingCartQuery);
    }

    @Override
    public void clean() {
        ShoppingCart shoppingCartQuery = new ShoppingCart();
        shoppingCartQuery.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCartQuery);
    }
}
